﻿package util {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.text.*;
	import toolbar.IntroSquare;
	import util.TextConvert;
	
	public class EventParser {
		public static var intro:String;
		public static var mouseX:int;
		public static var mouseY:int;
		public static var dispatcher:EventDispatcher;
		private var editor:Editor;
		
		public function EventParser(editor:Editor):void {
			this.editor = editor;
			dispatcher = new EventDispatcher();
			assignEventListener();
		}
		
		private function assignEventListener():void {
			//鼠标提示
			dispatcher.addEventListener(Config.OVER_INTRO, onOverHandler);
			dispatcher.addEventListener(Config.OUT_INTRO, onOutHandler);
			//按钮侦听
			dispatcher.addEventListener(Config.SOURCE_ON, onSourceOnHandler);
			dispatcher.addEventListener(Config.SOURCE_OFF, onSourceOffHandler);
			dispatcher.addEventListener(Config.UBB_ON, onUbbOnHandler);
			dispatcher.addEventListener(Config.UBB_OFF, onUbbOffHandler);
			dispatcher.addEventListener(Config.NEW, onNewHandler);
			dispatcher.addEventListener(Config.OPEN, onOpenHandler);
			dispatcher.addEventListener(Config.SAVE, onSaveHandler);
			dispatcher.addEventListener(Config.CUT, onCutHandler);
			dispatcher.addEventListener(Config.COPY, onCopyHandler);
			dispatcher.addEventListener(Config.PASTE, onPasteHandler);
			dispatcher.addEventListener(Config.BRUSH, onBrushHandler);
			dispatcher.addEventListener(Config.BOLD_ON, onBoldOnHandler);
			dispatcher.addEventListener(Config.BOLD_OFF, onBoldOffHandler);
			dispatcher.addEventListener(Config.ITALIC_ON, onItalicOnHandler);
			dispatcher.addEventListener(Config.ITALIC_OFF, onItalicOffHandler);
			dispatcher.addEventListener(Config.UNDERLINE_ON, onUnderlineOnHandler);
			dispatcher.addEventListener(Config.UNDERLINE_OFF, onUnderlineOffHandler);
			dispatcher.addEventListener(Config.LEFT, onLeftHandler);
			dispatcher.addEventListener(Config.CENTER, onCenterHandler);
			dispatcher.addEventListener(Config.RIGHT, onRightHandler);
			dispatcher.addEventListener(Config.UNDO, onUndoHandler);
			dispatcher.addEventListener(Config.REDO, onRedoHandler);
			dispatcher.addEventListener(Config.FONT_COLOR, onFontColorHandler);
			dispatcher.addEventListener(Config.FONT_SIZE, onFontSizeHandler);
			dispatcher.addEventListener(Config.FONT_FAMILY, onFontFamilyHandler);
		}
		
		//鼠标响应提示
		private function onOverHandler(event:Event):void {
			editor.showIntro(intro, mouseX, mouseY);
		}
		private function onOutHandler(event:Event):void {
			editor.hideIntro();
		}
		
		//各按钮响应
		private function onSourceOnHandler(event:Event):void {
			editor.sourceOn();
		}
		private function onSourceOffHandler(event:Event):void {
			editor.sourceOff();
		}
		private function onUbbOnHandler(event:Event):void {
			editor.ubbOn();
		}
		private function onUbbOffHandler(event:Event):void {
			editor.ubbOff();
		}
		private function onNewHandler(event:Event):void {
			editor.newFile();
		}
		private function onOpenHandler(event:Event):void {
			editor.open();
		}
		private function onSaveHandler(event:Event):void {
			editor.save();
		}
		private function onCutHandler(event:Event):void {
			editor.cut();
		}
		private function onCopyHandler(event:Event):void {
			editor.copy();
		}
		private function onPasteHandler(event:Event):void {
			editor.paste();
		}
		private function onBrushHandler(event:Event):void {
			editor.blush();
		}
		private function onBoldOnHandler(event:Event):void {
			editor.boldOn();
		}
		private function onBoldOffHandler(event:Event):void {
			editor.boldOff();
		}
		private function onItalicOnHandler(event:Event):void {
			editor.italicOn();
		}
		private function onItalicOffHandler(event:Event):void {
			editor.italicOff();
		}
		private function onUnderlineOnHandler(event:Event):void {
			editor.underlineOn();
		}
		private function onUnderlineOffHandler(event:Event):void {
			editor.underlineOff();
		}
		private function onLeftHandler(event:Event):void {
			editor.alignLeft();
		}
		private function onCenterHandler(event:Event):void {
			editor.alignCenter();
		}
		private function onRightHandler(event:Event):void {
			editor.alignRight();
		}
		private function onUndoHandler(event:Event):void {
			editor.undo();
		}
		private function onRedoHandler(event:Event):void {
			editor.redo();
		}
		private function onFontColorHandler(event:Event):void {
			editor.showColorPicker(mouseX, mouseY);
		}
		private function onFontSizeHandler(event:Event):void {
			editor.showSizeList(mouseX, mouseY);
		}
		private function onFontFamilyHandler(event:Event):void {
			editor.showFamilyList(mouseX, mouseY);
		}
		
		//按钮介绍
		public static function dispatchOver(s:String, mx:int, my:int):void {
			intro = s;
			mouseX = mx;
			mouseY = my;
			dispatcher.dispatchEvent(new Event(Config.OVER_INTRO));
		}
		public static function dispatchOut():void {
			dispatcher.dispatchEvent(new Event(Config.OUT_INTRO));
		}
		
		//所有normal button发送自己的intro为事件名称，select button根据状态发送intro或intro + 1
		public static function dispatch(type:String):void {
			dispatcher.dispatchEvent(new Event(type));
		}
	}
}